package com.cheer.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cheer.common.Page;
import com.cheer.dao.OaBannerDao;
import com.cheer.dto.OaBannerAddDTO;
import com.cheer.dto.OaBannerUpdateDTO;
import com.cheer.entity.OaBannerEntity;
import com.cheer.exception.ServiceException;
import com.cheer.service.OaBannerService;
import com.cheer.vo.ImageVO;
import com.cheer.vo.OaBannerInfoVO;
import com.cheer.vo.OaBannerListVO;
import com.cheer.vo.OaBannerSimpleVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service("oaBannerService")
public class OaBannerServiceImpl extends ServiceImpl<OaBannerDao, OaBannerEntity> implements OaBannerService {

    @Value("${image.url}")
    private String imageUrl;

    @Autowired
    private OaBannerDao oaBannerDao;

    @Override
    @Transactional
    public void add(OaBannerAddDTO bannerAddDTO) {
        if (bannerAddDTO.getSort() == null || bannerAddDTO.getSort() <= 0) {
            bannerAddDTO.setSort(1);
        }

        oaBannerDao.incrSort(bannerAddDTO.getSort());

        OaBannerEntity banner = new OaBannerEntity()
                .setName(bannerAddDTO.getName())
                .setImgUrl(bannerAddDTO.getImgUrl())
                .setHref(bannerAddDTO.getHref())
                .setSort(bannerAddDTO.getSort())
                .setStatus(bannerAddDTO.getStatus())
                .setType(bannerAddDTO.getType())
                .setItemsId(bannerAddDTO.getItemsId())
                .setAddTime(String.valueOf(DateUtil.currentSeconds()));

        oaBannerDao.insert(banner);
    }

    @Override
    public Page<OaBannerListVO> list(Integer pageNum, Integer pageSize) {
        IPage<OaBannerEntity> page = oaBannerDao.selectPage(pageNum, pageSize);

        List<OaBannerListVO> bannerListVOList = page.getRecords().stream()
                .map(banner -> new OaBannerListVO()
                        .setId(banner.getId())
                        .setName(banner.getName())
                        .setSort(banner.getSort())
                        .setImgUrl(imageUrl + banner.getImgUrl())
                        .setHref(banner.getHref())
                        .setStatus(banner.getStatus())
                        .setAddTime(banner.getAddTime()))
                .collect(Collectors.toList());

        return new Page<>(page, bannerListVOList);
    }

    @Override
    @Transactional
    public void delete(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }

        for (Long id : ids) {
            OaBannerEntity banner = oaBannerDao.selectById(id);
            if (banner != null) {
                oaBannerDao.deleteById(id);
                oaBannerDao.decrSort(banner.getSort());
            }
        }
    }

    @Override
    public OaBannerInfoVO info(Integer id) {
        OaBannerEntity banner = oaBannerDao.selectById(id);
        if (banner == null) {
            throw new ServiceException("轮播图不存在");
        }

        return new OaBannerInfoVO()
                .setId(banner.getId())
                .setName(banner.getName())
                .setSort(banner.getSort())
                .setHref(banner.getHref())
                .setStatus(banner.getStatus())
                .setType(banner.getType())
                .setItemsId(banner.getItemsId())
                .setImgUrl(new ImageVO()
                        .setFullUrl(imageUrl + banner.getImgUrl())
                        .setShortUrl(banner.getImgUrl()));
    }

    @Override
    @Transactional
    public void update(OaBannerUpdateDTO bannerUpdateDTO) {
        OaBannerEntity banner = oaBannerDao.selectById(bannerUpdateDTO.getId());
        if (banner == null) {
            throw new ServiceException("轮播图不存在");
        }

        if (bannerUpdateDTO.getSort() != null) {
            if (bannerUpdateDTO.getSort() <= 0) {
                bannerUpdateDTO.setSort(1);
            }
            if (banner.getSort() > bannerUpdateDTO.getSort()) {
                oaBannerDao.incrSortRange(bannerUpdateDTO.getSort(), banner.getSort());
            } else if (banner.getSort() < bannerUpdateDTO.getSort()) {
                oaBannerDao.decrSortRange(banner.getSort(), bannerUpdateDTO.getSort());
            }
        }

        OaBannerEntity updateBanner = new OaBannerEntity()
                .setId(bannerUpdateDTO.getId())
                .setName(bannerUpdateDTO.getName())
                .setImgUrl(bannerUpdateDTO.getImgUrl())
                .setHref(bannerUpdateDTO.getHref())
                .setType(bannerUpdateDTO.getType())
                .setItemsId(bannerUpdateDTO.getItemsId())
                .setSort(bannerUpdateDTO.getSort())
                .setStatus(bannerUpdateDTO.getStatus());

        oaBannerDao.updateById(updateBanner);
    }

    @Override
    public void updateStatus(Integer id, Integer status) {
        OaBannerEntity banner = oaBannerDao.selectById(id);
        if (banner == null) {
            throw new ServiceException("轮播图不存在");
        }

        OaBannerEntity updateBanner = new OaBannerEntity()
                .setId(banner.getId())
                .setStatus(status);

        oaBannerDao.updateById(updateBanner);
    }

    @Override
    public List<OaBannerSimpleVO> getFirst() {
        List<OaBannerEntity> bannerList = oaBannerDao.selectListByOrder();

        return bannerList.stream()
                .map(banner -> new OaBannerSimpleVO()
                        .setType(banner.getType())
                        .setItemsId(banner.getItemsId())
                        .setImgUrl(imageUrl + banner.getImgUrl())
                        .setHref(banner.getHref()))
                .collect(Collectors.toList());
    }
}